package com.xinghen.scheduler.client.executor;

import com.xinghen.scheduler.client.TriggerDataMapKey;
import com.xinghen.scheduler.client.TriggerType;
import com.xinghen.scheduler.client.domain.RmsJobParam;
import com.xinghen.scheduler.client.service.SchClientService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * @author xinghen
 * @ClassName: AbstractExecutor
 * @Package: com.xinghen.scheduler.client.executor
 * @Description AbstractExecutor
 * @create 2017-12-08 16:42
 */
public abstract class AbstractExecutor {

    @Autowired
    private SchClientService schClientService;

    /**
     * 执行方法
     *
     * @param param
     */
    public void handle(RmsJobParam param) {
        this.handle(param.getId(), param.getJobDataMap());
    }

    /**
     * 调用链 (允许并发,异步调用)
     *
     * @param parentId
     * @param serviceCode
     * @param beanName
     * @param param
     * @return
     */
    protected String chain(String parentId, String serviceCode, String beanName, Map<String, String> param) {
        return this.chain(true, parentId, serviceCode, beanName, true, param);
    }

    /**
     * 调用链
     *
     * @param isConcurrent 是否并发(true:允许并发,false:禁止并发)
     * @param parentId     父ID
     * @param serviceCode  服务代码
     * @param beanName     bean名称
     * @param async        是否异步
     * @param param        业务参数
     * @return 结果
     */
    protected String chain(boolean isConcurrent, String parentId, String serviceCode, String beanName, boolean async, Map<String, String> param) {
        //构建jobDataMap
        Map<String, String> jdm = new HashMap<>();
        jdm.put(TriggerDataMapKey.PARENT_TRIGGER_ID.value(), parentId);
        jdm.put(TriggerDataMapKey.TRIGGER_TYPE.value(), TriggerType.CALL_CHAIN.value());
        jdm.put(TriggerDataMapKey.SERVICE_CODE.value(), serviceCode);
        jdm.put(TriggerDataMapKey.BEAN_NAME.value(), beanName);
        jdm.put(TriggerDataMapKey.ASYNC.value(), Boolean.toString(async));
        jdm.putAll(param);
        // 调用 & 返回
        String jobCode = isConcurrent ? "rmsJob" : "rmsJobDisallowConcurrent";
        return this.schClientService.trigger(jobCode, jdm);
    }

    /**
     * 执行方法
     *
     * @param id id
     * @param jobDataMap jobDataMap
     */
    protected abstract void handle(String id, Map<String, Object> jobDataMap);

}
